//
// DbSettings.cs
// 
// Handles the database connection settings.
//
// Author:
//   Radu Tofoleanu <kenjiru.ro@gmail.com>
//
// Copyright (C) 2007-2008 Radu Tofoleanu (http://kenjiru.ro)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.IO;
using System.Xml.Serialization;

namespace EigaLib
{
	[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
	public class DbSettings
	{
		private string settingsFileName = @"settings2.xml";
		// info. privind conectarea la DB
		private string hostName;
		private string dbName;
		private string userName;
		private string password;
		// info. privind autent. aplicatiei
		private string appUserName;
		private string appPassword;
	
		[System.Xml.Serialization.XmlElementAttribute()]
		public string HostName
		{
			get { return hostName; }
			set { hostName = value; }
		}
		[System.Xml.Serialization.XmlElementAttribute()]
		public string DbName
		{
			get { return dbName; }
			set { dbName = value; }
		}
		[System.Xml.Serialization.XmlElementAttribute()]
		public string UserName
		{
			get { return userName; }
			set { userName = value; }
		}
		[System.Xml.Serialization.XmlElementAttribute()]
		public string Password
		{
			get { return password; }
			set { password = value; }
		}
		[System.Xml.Serialization.XmlElementAttribute()]
		public string AppUserName
		{
			get { return appUserName; }
			set { appUserName = value; }
		}
		[System.Xml.Serialization.XmlElementAttribute()]
		public string AppPassword
		{
			get { return appPassword; }
			set { appPassword = value; }
		}
		
		public void Load ()
		{
			// incarcam setarile din fisierul de configurare, daca exista
			DbSettings dbSettings = new DbSettings ();
			
			try {
				FileStream f = new FileStream(this.settingsFileName, FileMode.Open);
				
				// incercam sa incarcam setarile / deserializam
				XmlSerializer sr = new XmlSerializer (typeof (DbSettings));
				dbSettings = (DbSettings) sr.Deserialize (f);
				f.Close ();
			} catch (FileNotFoundException ex) {
				dbSettings.MakeDefault ();
				Console.WriteLine ("EigaDb.cs: EigaDb(): Exception: " + ex.Message);
			}
			
			this.hostName = dbSettings.HostName;
			this.dbName = dbSettings.DbName;
			this.userName = dbSettings.UserName;
			this.password = dbSettings.Password;
			this.appPassword = dbSettings.AppPassword;
			this.appUserName = dbSettings.AppUserName;
		}
		
		public void Save ()
		{
			// serializam ob. DbSettings
			TextWriter tr = new StreamWriter (this.settingsFileName);
			XmlSerializer sr = new XmlSerializer (typeof (DbSettings));
			sr.Serialize (tr, this);
			tr.Close ();
		}
		
		private void MakeDefault ()
		{
			// setarile implicite
			this.hostName = "10.10.13.2";
			this.dbName = "XE";
			this.userName = "Radu";
			this.password = "panamea";
			this.appUserName = "ghita";
			this.appPassword = "portita";
		}
	}
}
